package com.zby.business.fragment;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.zby.business.R;
import com.zby.business.net.MyAsyncTask;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.zip.Inflater;

/**
 * Created by BY on 2016/4/11.
 */
public class UserInfo extends Activity {
    private ListView userListView;
    private LayoutInflater inflater;
    private ArrayList username, password, account, name, phone, email, sex, idcard, address;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.userinfo);
        init();
        super.onCreate(savedInstanceState);
    }

    private void init() {
        inflater = LayoutInflater.from(getApplicationContext());
        username = new ArrayList();
        password = new ArrayList();
        account = new ArrayList();
        name = new ArrayList();
        phone = new ArrayList();
        email = new ArrayList();
        sex = new ArrayList();
        idcard = new ArrayList();
        address = new ArrayList();
        userListView = (ListView) findViewById(R.id.userInfoListView);
        MyAsyncTask task = new MyAsyncTask();
        try {
            String s = task.execute("getUsers").get();
            System.out.println(s);
            JSONObject object = new JSONObject(s);
            JSONArray users = object.getJSONArray("user");
            JSONArray details = object.getJSONArray("userDetails");
            for (int i = 0; i < users.length(); i++) {
                JSONObject user = (JSONObject) users.get(i);
                JSONObject detail = (JSONObject) details.get(i);
                username.add(user.get("username"));
                password.add(user.get("password"));
                account.add(user.get("account"));
                name.add(detail.get("name"));
                phone.add(detail.get("phone"));
                email.add(detail.get("email"));
                sex.add(detail.get("sex"));
                address.add(detail.get("address"));
                idcard.add(detail.get("idcard"));
            }
            userListView.setAdapter(new BaseAdapter() {
                @Override
                public int getCount() {
                    return name.size();
                }

                @Override
                public Object getItem(int position) {
                    return name.get(position);
                }

                @Override
                public long getItemId(int position) {
                    return position;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
                    convertView = inflater.inflate(R.layout.textview, null);
                    TextView textView = (TextView) convertView.findViewById(R.id.commentTV);
                    textView.setText("" + username.get(position));
                    return convertView;
                }
            });
            userListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    AlertDialog dialog = new AlertDialog.Builder(UserInfo.this).create();
                    View view1 = inflater.inflate(R.layout.textview, null);
                    TextView textView = (TextView) view1.findViewById(R.id.commentTV);
                    textView.setText("用户名：" + username.get(position) + "\n密码：" + password.get(position) + "\n姓名：" + name.get(position) +"性别："+sex.get(position) +"\n电话：" + phone.get(position) + "\n地址：" + address.get(position) + "\n身份证：" + idcard.get(position) + "\n账户：" + account.get(position) + "\n邮箱：" + email.get(position));
                    dialog.setView(view1);
                    dialog.show();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
